package com.yuanduo_app.window;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.xujl.fastlib.utils.StringUtils;

import com.yuanduo_app.R;

public class CommonInfoWindow extends BaseWindow implements View.OnClickListener {
    public static final int LEFT_BTN = 0;
    public static final int RIGHT_BTN = 1;
    public static final int CANCEL = 2;
    private TextView mInfoTv;
    private TextView mTitleTv;
    private TextView mLeftTv;
    private TextView mRightTv;
    private TextView mErrorTv;
    private View mBtnLine;
    private Builder mBuilder;
    private Callback mCallback;
    private boolean isChoose;

    public CommonInfoWindow (Context context, Builder builder) {
        super(context);
        setPopupGravity(Gravity.CENTER);
        setOutSideDismiss(false);
        mBuilder = builder;
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss () {
                if (!isChoose) {
                    if (mCallback != null) {
                        mCallback.onWindowClick(CANCEL);
                    }
                }
            }
        });
    }

    @Override
    public View onCreateContentView () {
        final View view = createPopupById(R.layout.window_common_info);
        mTitleTv = view.findViewById(R.id.tv_title);
        mInfoTv = view.findViewById(R.id.tv_info);
        mLeftTv = view.findViewById(R.id.tv_left);
        mErrorTv = view.findViewById(R.id.tv_error);
        mBtnLine = view.findViewById(R.id.line);
        mLeftTv.setOnClickListener(this);
        mRightTv = view.findViewById(R.id.tv_right);
        mRightTv.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick (View view) {
        isChoose = true;
        dismiss();
        switch (view.getId()) {
            case R.id.tv_right:
                if (mCallback != null) {
                    mCallback.onWindowClick(RIGHT_BTN);
                }
                break;
            case R.id.tv_left:
                if (mCallback != null) {
                    mCallback.onWindowClick(LEFT_BTN);
                }
                break;
            default:

                break;

        }
    }

    @Override
    public void showPopupWindow () {
        super.showPopupWindow();
        isChoose = false;
        if (mBuilder != null) {
            if (!StringUtils.isEmpty(mBuilder.title)) {
                mTitleTv.setText(mBuilder.title);
            }
            if (!StringUtils.isEmpty(mBuilder.info)) {
                mInfoTv.setText(mBuilder.info);
            }
            mInfoTv.setGravity(mBuilder.getGravity());
            if (mBuilder.isSingleBtn()) {
                mLeftTv.setVisibility(View.GONE);
                mBtnLine.setVisibility(View.GONE);
            } else {
                mLeftTv.setVisibility(View.VISIBLE);
                mBtnLine.setVisibility(View.VISIBLE);
            }
            if (mBuilder.spannableInfo != null) {
                mInfoTv.setText(mBuilder.spannableInfo);
                mInfoTv.setMovementMethod(LinkMovementMethod.getInstance());
                mInfoTv.setHighlightColor(Color.TRANSPARENT);
            }
            if (!StringUtils.isEmpty(mBuilder.leftStr)) {
                mLeftTv.setText(mBuilder.leftStr);
            }
            if (!StringUtils.isEmpty(mBuilder.rightStr)) {
                mRightTv.setText(mBuilder.rightStr);
            }
            mErrorTv.setText(mBuilder.errorMsg);
        }
    }

    public void setCallback (Callback callback) {
        mCallback = callback;
    }

    public static Builder createBuilder () {
        return new Builder();
    }

    public static class Builder {
        private String title;
        private String info;
        private SpannableStringBuilder spannableInfo;
        private String leftStr;
        private String rightStr;
        private String errorMsg;
        private int gravity = Gravity.CENTER;
        private boolean singleBtn;

        public SpannableStringBuilder getSpannableInfo () {
            return spannableInfo;
        }

        public Builder setSpannableInfo (SpannableStringBuilder spannableInfo) {
            this.spannableInfo = spannableInfo;
            return this;
        }

        public int getGravity () {
            return gravity;
        }

        public Builder setGravity (int gravity) {
            this.gravity = gravity;
            return this;
        }

        public boolean isSingleBtn () {
            return singleBtn;
        }

        public Builder setSingleBtn (boolean singleBtn) {
            this.singleBtn = singleBtn;
            return this;
        }

        public String getTitle () {
            return title;
        }

        public Builder setTitle (String title) {
            this.title = title;
            return this;
        }

        public String getInfo () {
            return info;
        }

        public Builder setInfo (String info) {
            this.info = info;
            return this;
        }

        public String getLeftStr () {
            return leftStr;
        }

        public Builder setLeftStr (String leftStr) {
            this.leftStr = leftStr;
            return this;
        }

        public String getRightStr () {
            return rightStr;
        }

        public Builder setRightStr (String rightStr) {
            this.rightStr = rightStr;
            return this;
        }

        public String getErrorMsg () {
            return errorMsg;
        }

        public Builder setErrorMsg (String errorMsg) {
            this.errorMsg = errorMsg;
            return this;
        }
    }

    public interface Callback {
        void onWindowClick (int code);
    }
}
